package com.eeepay.eeepay_v2.activity;

import android.Manifest;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.api.CheckVersionRequestBuilder;
import com.eeepay.eeepay_v2.util.NetworkUtils;
import com.eeepay.eeepay_v2.util.VersionUtils;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.util.CheckPermissionUtil;

/**
 * Created by zw on 2016/3/8 0008.
 */
public class AboutActivity extends ABBaseActivity {
    final int RESULTCODE_100 = 100;
    private static final String[] PERMISSIONS = new String[]{
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
    };
    /**
     * 盛代宝介绍文本 用的是weburl 地址
     **/
    private final String WEBURL = "http://www.eeepay.cn/sdb-txt.html";
    private ImageView appIcon;
    private TextView appName;
    private TextView tv_versionCode;
    private WebView web_view_tip;//盛代宝介绍文本
    private TextView tv_checkUpdate;//检查更新
    @Override
    protected int getLayoutId() {
        return R.layout.activity_about;
    }

    @Override
    protected void initWidget() {
        appIcon = getViewById(R.id.iv_app_icon);
        appName = getViewById(R.id.tv_app_name);
        web_view_tip = getViewById(R.id.web_view_tip);
        /** 初始化websetting **/
        inintWebSetting();
        tv_checkUpdate = getViewById(R.id.tv_checkUpdate);
        tv_versionCode = getViewById(R.id.tv_version_code);
        tv_versionCode.setText(VersionUtils.getVersionName(mContext));
    }

    /**
     * 初始化websetting
     */
    private void inintWebSetting() {
        /** 提高渲染的优先级**/
        web_view_tip.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        web_view_tip.getSettings().setDefaultTextEncodingName("utf-8");//设置编码格式
        /** 设置缓存模式**/
        if (NetworkUtils.checkNet(getApplicationContext())) {
            web_view_tip.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);//根据cache-control决定是否从网络上取数据。
        } else {
            web_view_tip.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);//没网，则从本地获取，即离线加载
        }
        /** 设置webview 字体大小**/
        web_view_tip.getSettings().setTextSize(WebSettings.TextSize.SMALLER);
        /** 设置H5的缓存是否打开，默认关闭**/
        web_view_tip.getSettings().setAppCacheEnabled(false);
        /**
         * 事件监听对象(WebViewClient) 处理各种通知、拦截请求事件的
         */
        web_view_tip.setWebViewClient(new WebViewClientObject());
        web_view_tip.loadUrl(WEBURL);
    }


    /**
     * 事件监听对象(WebViewClient) 处理各种通知、拦截请求事件的，
     */
    protected class WebViewClientObject extends WebViewClient {
        /**
         * 对网页中超链接按钮的响应
         **/
        @Override
        public boolean shouldOverrideUrlLoading(final WebView webView, String url) {
            web_view_tip.loadUrl(url);
            return true;
        }

        /**
         * 载入页面开始的事件
         **/
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        /**
         * 载入页面完成的事件
         **/
        @Override
        public void onPageFinished(WebView webView, String url) {
            super.onPageFinished(webView, url);
        }

        @Override
        public void onReceivedSslError(WebView webView, SslErrorHandler sslErrorHandler, SslError sslError) {
            sslErrorHandler.proceed();// 接受证书
            super.onReceivedSslError(webView, sslErrorHandler, sslError);
        }

    }


    @Override
    protected void eventOnClick() {
        //add by zhuangzeqin 2017年4月17日15:16:38  检查更新
        tv_checkUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showProgressDialog();
                setPermission();

            }
        });
    }

    private void setPermission() {
        try {
            CheckPermissionUtil.listener = new CheckPermissionUtil.onPermissionListener() {
                @Override
                public void onSuccess() {
                    //开始手动发送请求app 升级的信息；
                    CheckVersionRequestBuilder.with(mContext).setManualCheck(true).setResultCallBack(new CheckVersionRequestBuilder.ResultCallBack() {
                        @Override
                        public void onMessage(String msg) {
                            dismissProgressDialog();
                            showToast(msg);
                        }

                        @Override
                        public void onForce(boolean isForce) {
                            dismissProgressDialog();
                        }
                    }).startCheckVersion();//开始检查版本升级
                }

                @Override
                public void onFailure() {
                    Toast.makeText(mContext, getString(R.string.permission_denied), Toast.LENGTH_SHORT).show();
                }
            };

            CheckPermissionUtil.checkPermission(this, PERMISSIONS, null, RESULTCODE_100);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == RESULTCODE_100) {
            CheckPermissionUtil.verifyPermissions(grantResults);
        }
    }
}
